﻿using NbMeterServices.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NbMeterServices
{
    public class WaterBillCalc
    {

        /// <summary>
        /// 根据水量计算水费
        /// </summary>
        /// <param name="waterUsage"></param>
        /// <param name="tiers"></param>
        /// <returns></returns>
        public static decimal CalculateBill(decimal waterUsage, List<TierePricing> tiers)
        {
            decimal totalBill = 0m;
            decimal water = waterUsage;

            // 遍历阶梯价格列表  
            decimal value = 0m;

            foreach (var tier in tiers)
            {
                if (water < tier.Threshold)
                {
                    totalBill += (water - value) * tier.Price;
                    break;
                }
                else
                {
                    totalBill += (tier.Threshold - value) * tier.Price;
                }

                value = tier.Threshold;
            }

            return totalBill;
        }


        /// <summary>
        /// 根据总金额计算水量
        /// </summary>
        /// <param name="totalMoney"></param>
        /// <param name="tiers"></param>
        /// <returns></returns>
        public static decimal CalculateWater(decimal totalMoney, List<TierePricing> tiers)
        {
            decimal totalwater = 0m;
            var money = totalMoney;

            decimal value = 0m;

            // 遍历阶梯价格列表 
            foreach (var tier in tiers)
            {
                if (money > (tier.Threshold - value) * tier.Price)
                {
                    money -= (tier.Threshold - value) * tier.Price;
                    totalwater += (tier.Threshold - value);
                }
                else
                {
                    totalwater += money / tier.Price;
                    break;
                }

                value = tier.Threshold;
            }

            return totalwater;
        }

        //public static decimal CalculateWater(decimal totalMoney, List<TierePricing> tiers)
        //{
        //    decimal totalwater = 0m;
        //    var money = totalMoney;

        //    // 遍历阶梯价格列表  
        //    decimal value = 0m;

        //    foreach (var tier in tiers)
        //    {
        //        if (totalMoney > tier.Threshold * tier.Price)
        //        {
        //            money -= (tier.Threshold - value) * tier.Price;
        //            totalwater += (tier.Threshold - value);
        //        }
        //        else
        //        {
        //            totalwater += money / tier.Price;
        //            break;
        //        }

        //        value = tier.Threshold;
        //    }

        //    return totalwater;
        //}
    }
}
